<?php
namespace app\index\controller;

use app\index\model\ClassFor;
use app\index\model\College;
use app\index\model\Major;
use app\index\model\School;
use app\index\model\Student;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        $school = School::all();
        $student = Student::all();
        $college = College::all();
        $major = Major::all();
        $class = ClassFor::all();
        return $this->fetch('index/index', [
            'status' => true,
            'data1' => $school,
            'data2' => $student,
            'data3' => $college,
            'data4' => $major,
            'data5' => $class
        ]);

    }

    public function getCollege() {
        $college = College::all();
        return json([
            'data' => $college
        ]);
    }

    public function getClass() {
        $class = ClassFor::all();
//        dump($class);
        return json([
            'data' => $class
        ]);
    }

    public function getTable() {
        $table = Student::where('school', '=', 'XNMD')->select();
        return json([
            'data' => $table
        ]);
    }

    public function getSchool() {
        $school = School::all();
        return json([
           'schoolMatch' => $school
        ]);
    }

    public function visitClass01() {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt ($ch, CURLOPT_URL, 'http://ec2-3-16-161-227.us-east-2.compute.amazonaws.com/course/linkage_online/ajax.php?type=5&father_id=1601');
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
//        echo $file_contents;
        $html = json_decode($file_contents);
//        dump($html);
        return json([
            'class01' => $html
        ]);
    }

    public function visitClass02() {
        $data=array('foo'=>'bar');
        $data=http_build_query($data);

        $opts=array(
            'http'=>array(
                'method'=>'POST',
                'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                    "Content-Length: ".strlen($data)."\r\n",
                'content'=>$data
            ),
        );
        $context=stream_context_create($opts);
        $html=file_get_contents('http://ec2-3-16-161-227.us-east-2.compute.amazonaws.com/course/linkage_online/ajax.php?type=5&father_id=1602',false,$context);
//        echo$html;
        $html = json_decode($html);
        return json([
           'class02' => $html
        ]);
    }

    public function visitMember22() {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt ($ch, CURLOPT_URL, 'http://ec2-3-16-161-227.us-east-2.compute.amazonaws.com/course/linkage_online/ajax.php?type=6&father_id=22');
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
//        echo $file_contents;
//        return json([
//            'member22' => $file_contents
//        ]);
        return $file_contents;
    }
}
